
var user_core_username;
var user_core_token;
$(document).ready(function() {
    // 登陆相关
    user_core_username = $.cookie('user_core_username');
    user_core_token = $.cookie('user_core_token');
    if (!user_core_username || !user_core_token) {
        $('#loginModal').modal({show:true,backdrop:'static',keyboard:false});
        $('.login_close').hide();
    } else {
        $('#loginModal').modal('hide');
        $(".login_username").text(": "+ user_core_username);
    }

    // 登陆
    $(".login_submit").on("click", function() {
        loginFrom("api/user_core/login.json", function(data){
            if (data && data.username) {
                $.cookie('user_core_username', data.username, { expires: 28 });
                if (data.token) {
                    $.cookie('user_core_token', data.token, { expires: 28 });
                }
                $(".login_username").text(": "+ data.username);
                user_core_username = data.username;
                
                // 判断 login_action 是否为函数，如果是则执行
                if (typeof login_action === 'function') {
                    login_action(data.username);
                }
            }
        })
    });
    // 注销
    $(".login_out").on("click", function() {
        $(".login_username").text("");
        loginOut("api/user_core/logout.json", function(data){
            $.cookie('user_core_username', '', { expires: 0 });
            $.cookie('user_core_token', '', { expires: 0 });
            $('#loginModal').modal({show:true,backdrop:'static',keyboard:false});
            $('#loginModal .prompt').html('<span></span>');
            $('.login_close').hide();
        })
    });
    
});